/**
 * Copyright(C) 2012 Luvina
 * MstGroupDaoImpl.java, 27-05-2012 NguyenVietDung
 */
package net.luvina.manageuser.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.transform.Transformers;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;

import net.luvina.manageuser.dao.MstGroupDao;
import net.luvina.manageuser.hibernate.MstGroup;
import net.luvina.manageuser.hibernate.MstJapan;

/**
 * MstGroupDaoImpl: Implement MstGroupDao để Xử lý Thao tác với DB bảng
 * mst_group
 *
 * @author NguyenVietDung
 */
@Repository("mstGroupDao")
public class MstGroupDaoImpl extends GenericDaoImpl<MstGroup, Integer> implements MstGroupDao {

	/**
	 * @param type
	 */
	public MstGroupDaoImpl() {
		super(MstGroup.class);
		// TODO Auto-generated constructor stub
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see net.luvina.manageuser.dao.MstGroupDao#getListGroup()
	 */
	@Override
	public List<MstGroup> getListGroup() {
		// TODO Auto-generated method stub
		// Execute sql command to get group object
		HibernateCallback callback = new HibernateCallback() {
			@Override
			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				StringBuilder sqlCommand = new StringBuilder("");
				// Select statement
				sqlCommand = sqlCommand.append("SELECT ");
				sqlCommand = sqlCommand.append("group_id as groupId, ");
				sqlCommand = sqlCommand.append("group_name as groupName ");
				// From statement
				sqlCommand = sqlCommand.append("FROM mst_group");
				// Where statement

				// Order by statement
				Query query = session.createSQLQuery(sqlCommand.toString()).addScalar("groupId", Hibernate.INTEGER).addScalar("groupName",
						Hibernate.STRING).setResultTransformer(Transformers.aliasToBean(MstGroup.class));
				return (List<MstGroup>) query.list();
			}

		};
		return (List<MstGroup>) getHibernateTemplate().execute(callback);
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see net.luvina.manageuser.dao.MstGroupDao#existMstGroup(int)
	 */
	@Override
	public boolean existMstGroup(final int groupId) {
		HibernateCallback callback = new HibernateCallback() {
			/*
			 * (non-Javadoc)
			 *
			 * @see
			 * org.springframework.orm.hibernate3.HibernateCallback#doInHibernate
			 * (org.hibernate.Session)
			 */
			@Override
			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				StringBuilder sqlCommand = new StringBuilder();
				sqlCommand.append(" FROM MstGroup mg ");
				sqlCommand.append(" WHERE mg.groupId=:groupId");
				Query query = session.createQuery(sqlCommand.toString());
				query.setString("groupId", String.valueOf(groupId));
				List<MstGroup> lsMstGroups = (List<MstGroup>) query.list();
				boolean isExist = false;
				if (lsMstGroups.size() == 0) {
					isExist = false;
				} else if (lsMstGroups.size() > 0) {
					isExist = true;
				}
				return isExist;
			}
		};
		return getHibernateTemplate().execute(callback);

	}
}
